﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.Collections.Generic;
using HRProject.Code.DTO;
using System.Globalization;

namespace HRProject.Asset
{
    public partial class EditAssetType : System.Web.UI.Page
    {
        private Guid typeId = Guid.Empty;
        private List<BLAsset> assets;

        public List<BLAsset> Assets
        {
            get { return assets; }
            set { assets = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            assets = new List<BLAsset>();
            if (!IsPostBack)
            {
                string id = Request.Params["id"];
                try
                {
                    typeId = new Guid(id);
                }
                catch (Exception exp)
                {
                    Console.Out.Write(exp.StackTrace);
                    typeId = Guid.Empty;
                }
                AssetTypeDAO dao = new AssetTypeDAO();
                BLAssetType filter = new BLAssetType();
                filter.Id = typeId;
                filter.Name = string.Empty;
                filter.Parent = new BLAssetType();
                filter.Parent.Id = Guid.Empty;
                List<BLAssetType> departments = dao.search(filter);
                if (departments.Count > 0)
                {
                    hdfId.Value = typeId.ToString();
                    tbxName.Text = departments[0].Name;
                    tbxParent.Text = departments[0].Parent.Name;
                    txaDescription.Text = departments[0].Description;

                    AssetDAO assetDAO = new AssetDAO();
                    BLAsset assetFilter = new BLAsset();
                    assetFilter.Id=Guid.Empty;
                    assetFilter.Name=string.Empty;
                    assetFilter.Type=new BLAssetType();
                    assetFilter.Type.Id=typeId;
                    assets = assetDAO.search(assetFilter);
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsEditAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            BLAssetType assetType = new BLAssetType();
            try
            {
                typeId = new Guid(hdfId.Value);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                typeId = Guid.Empty;
            }
            assetType.Id = typeId;
            assetType.Name = tbxName.Text;
            assetType.Description = txaDescription.Text;
            assetType.DataSignature = 0;

            AssetTypeDAO dao = new AssetTypeDAO();
            dao.update(assetType);

            string[] assetIds = Request.Params.GetValues("assetId");
            string[] removedAssetIds = Request.Params.GetValues("removedAssetId");
            string[] assetNames = Request.Params.GetValues("assetName");
            string[] assetDescriptions = Request.Params.GetValues("assetDescription");
            string[] assetIconUrls = Request.Params.GetValues("assetImageUrl");
            string[] assetProductIds = Request.Params.GetValues("assetProductId");
            string[] assetImportDates = Request.Params.GetValues("assetImportDate");
            string[] assetTerminateDates = Request.Params.GetValues("assetTerminateDate");

            AssetDAO assetDAO = new AssetDAO();
            if (removedAssetIds != null)
            {
                for (int i = 0; i < removedAssetIds.Length; i++)
                {
                    BLAsset asset = new BLAsset();
                    try
                    {
                        asset.Id = new Guid(removedAssetIds[i]);
                        int errorCode = 0;
                        assetDAO.Delete(asset, ref errorCode);
                    }
                    catch (Exception exp)
                    {
                        Console.Write(exp.StackTrace);
                    }
                }
            }
            for (int i = 0; i < assetNames.Length; i++)
            {
                BLAsset asset = new BLAsset();
                try
                {
                    asset.Id = new Guid(assetIds[i]);
                }
                catch (Exception exp)
                {
                    Console.Out.WriteLine(exp.Message);
                    asset.Id = Guid.Empty;
                }
                asset.Name = assetNames[i];
                asset.Type = assetType;
                asset.ProductId = assetProductIds[i];
                asset.Description = assetDescriptions[i];
                asset.ImageUrl = assetIconUrls[i];
                DateTime date;
                if (DateTime.TryParseExact(assetImportDates[i], Constant.DateTimeFormat, Constant.enCulture, DateTimeStyles.None, out date))
                {
                    asset.ImportDate = date;
                }
                if (DateTime.TryParseExact(assetTerminateDates[i], Constant.DateTimeFormat, Constant.enCulture, DateTimeStyles.None, out date))
                {
                    asset.TerminateDate = date;
                }

                int result2 = 0;
                if (asset.Id.Equals(Guid.Empty))
                {
                    result2 = assetDAO.insert(asset);
                }
                else
                {
                    result2 = assetDAO.update(asset);
                }
            }

            Util.CloseFrame(Response);
        }
    }
}
